package edu.luc.etl.jbunit.tl;

/**
 * The formula <code>p since q</code>
 * specifies that <code>q</code> must have been true sometime in the past
 * history of this system run, and that <code>p</code> must have been true
 * in every time unit since the last time <code>q</code> was true.
 *
 * @see edu.luc.etl.jbunit.tl.BackTo
 */

public class Since<T> extends Expr<T> { // p Since q

  private Expr<T> p, q;

  public Since(Expr<T> p, Expr<T> q) {
    this.p = p;
    this.q = q;
    val = false; // KL: this seems to be required
  }

  public Since(T p, T q) {
    this(new Leaf<T>(p), new Leaf<T>(q));
  }

  public Since(Expr<T> p, T q) {
    this(p, new Leaf<T>(q));
  }

  public Since(T p, Expr<T> q) {
    this(new Leaf<T>(p), q);
  }

  public Since<T> clone() {
    Since<T> result = (Since<T>) super.clone();
    result.p = p.clone();
    result.q = q.clone();
    return result;
  }

  public void reset () { val = false; p.reset(); q.reset(); }

  public void tick () { evaluated = false; p.tick(); q.tick(); }

  boolean ev () {
    boolean cp = p.valuate();
    boolean cq = q.valuate();

    val = cq || (val && cp);
    return val;
  }

  void accept(Visitor<T> v) {
    v.visitSince(this);
    p.accept(v);
    q.accept(v);
  }
}

